package cn.itcast.up.web33.up_web33.controller;


import cn.itcast.up.web33.up_web33.bean.HttpResult;
import cn.itcast.up.web33.up_web33.bean.dto.TagDto;
import cn.itcast.up.web33.up_web33.bean.dto.TagModel;
import cn.itcast.up.web33.up_web33.service.TagService;
import cn.itcast.up.web33.up_web33.util.HttpCodes;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class TagController {

    @Autowired
    private TagService tagService;


    /**
     * 插入123级标签
     * @param list
     * @return
     */
    @PutMapping("tags/relation")
    @ResponseBody
    public HttpResult saveTags(@RequestBody List<TagDto> list){
        System.out.println(list);
        //将标签存入数据库
        tagService.saveTags(list);
        HttpResult result = new HttpResult(HttpCodes.SUCCESS, "数据插入成功", null);
        String json = JSON.toJSONString(result);
        System.out.println(json);
        return result;
    }

    @RequestMapping("tags")
    public HttpResult findByPidAndLevel(Long pid, Integer level){
        //如果级别等于空,就按照pid查询,否则就按照等级查询
        List<TagDto> list = null;
        if (pid != null){
            //按照pid查询
            list = tagService.findByPid(pid);
        }
        if (level != null) {
            list = tagService.findByLevel(level);
        }
        HttpResult<List<TagDto>> result = new HttpResult<>(HttpCodes.SUCCESS, "数据查询成功", list);
        String json = JSON.toJSONString(result);
        System.out.println(json);
        return result;
    }

    //获取4级标签创建的代码
    @PutMapping("tags/model")
    public void saveTagModel(@RequestBody TagModel tagModel){
        //获取对应的标签和模型
        tagService.saveTagModel(tagModel.getTag(), tagModel.getModel());
    }

    //获取4级标签列表
    @GetMapping("tags/model")
    public HttpResult getTagModel(Long pid) {
        //根据3级Pid获取所有的4级的标签和模型

        List<TagModel> list = null;
        list = tagService.findAllTagModel(pid);
        HttpResult<List<TagModel>> result = new HttpResult<>(HttpCodes.SUCCESS, "查询成功", list);
        String json = JSON.toJSONString(result);
        System.out.println(json);
        return result;
    }

}
